from  ConfigParser import ConfigParser
from os import getenv, access, R_OK 
from os.path import join, exists
from logging import warn, debug, info, critical
import sys
conf = {}

def getconfig():
    config_file = join (getenv('HOME'), '.shui.rc')
    if conf == {}:
        if not exists(config_file):
            info('building configuration')
            makeconfig(config_file)
        if exists(config_file) and not access(config_file, R_OK):
            critical('config file not accessible running with defaults')
            return 
        
        cfg = ConfigParser()
        rd = cfg.read(config_file)
        for section in cfg.sections():
            conf[section] = dict(cfg.items(section))
                
    return



def makeconfig(file):
    fd = open(file, 'w')
    str = '''
[Common]
name = My Server
source_type = file
[FileSource]
source_path = /home/<username>/Music
'''
    fd.write(str)
    fd.close()
    print "Modify ~/.shui.rc and run again"
    sys.exit(1)
